package com.ubing.boot.notesforjava.test.file;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class FileChannelTransferTo {


    public static void main(String[] args) {

        try (
                FileChannel from = new FileInputStream("from.txt").getChannel();
                FileChannel to = new FileOutputStream("to.txt").getChannel();
        ) {

            // 效率高  transferTo 会引用操作系统零拷贝进行优化 ， 上限（2G）

            long size = from.size();
            for (long left = size; left>0;) {
                left-=from.transferTo(size-left, left, to);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
